﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Dawn_of_Man
{
    public class DomainCreator
    {
        Random random = new Random();
        Tribe t = null;
        List<Ressource> ressources = new List<Ressource>();
        public Tribe generateTribe()
        {
            t = new Tribe();
            t.tribe = new List<TribesMan>();
            List<String> tribesmanNames = GenerateNames();
            Chieftain chief = new Chieftain();
            chief.name = RandomName(tribesmanNames);
            t.chief = chief;
            for (int i = 0; i < 10; i++)
            {
                TribesMan man = new TribesMan();
                man.name = RandomName(tribesmanNames);
                man.age = random.Next(15, 25);
                man.health = 72;
                man.hunger = 60;
                if (i % 2 == 0)
                    man.sex = true;
                else
                    man.sex = false;
                man.assignment = getAssignmentsList().ElementAt<Assignment>(0);
                t.tribe.Add(man);
            }
            Village v = new Village();
            v.name = "Ugh!";
            t.village = v;
            v.buildings = new List<Building>();
            StorageHut storage = new StorageHut();
            storage.name = "Storage Hut";
            storage.size = 1;
            storage.constructionBlocks = 3;
            storage.constructionWood = 3;
            storage.constructionTools = 2;
            storage.construction = 100;
            v.storageHut = storage;

            CreateRessources(v);
            //Temp! Add a tool makers hut
                v.buildings.Add(getBuildingsList().ElementAt<Building>(0));
                v.buildings.Add(getBuildingsList().ElementAt<Building>(1));
                v.buildings.Add(getBuildingsList().ElementAt<Building>(2));
            
            v.areas = new List<Area>();
            foreach (Area a in getAreaList())
            {
                v.areas.Add(a);
            }

            return t;
        }

        private void CreateRessources(Village v)
        {
            Ressource clay = new Ressource();
            clay.name = "Clay";
            ressources.Add(clay);
            cloneRessource(clay, 5, v.storageHut);

            Ressource hide = new Ressource();
            hide.name = "Hide";
            ressources.Add(hide);
            cloneRessource(hide, 5, v.storageHut);

            Ressource meat = new Ressource();
            meat.name = "Meat";
            ressources.Add(meat);
            cloneRessource(meat, 5, v.storageHut);

            Ressource stone = new Ressource();
            stone.name = "Stone";
            ressources.Add(stone);
            cloneRessource(stone, 5, v.storageHut);

            Ressource water = new Ressource();
            water.name = "Water";
            ressources.Add(water);
            cloneRessource(water, 5, v.storageHut);

            Ressource wheat = new Ressource();
            wheat.name = "Wheat";
            ressources.Add(wheat);
            cloneRessource(wheat, 5, v.storageHut);

            Ressource wood = new Ressource();
            wood.name = "Wood";
            ressources.Add(wood);
            cloneRessource(wood, 30, v.storageHut);

            Ressource tool = new Ressource();
            tool.name = "Tool";
            ressources.Add(tool);
            cloneRessource(tool, 30, v.storageHut);

            Ressource block = new Ressource();
            block.name = "Block";
            ressources.Add(block);
            cloneRessource(block, 30, v.storageHut);
        }

        private List<String> GenerateNames()
        {
            List<String> tribesmanNames = new List<String>();
            try
            {
                using (StreamReader sr = new StreamReader(@"names.txt"))
                {
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        tribesmanNames.Add(line);
                    }
                }
            }
            catch (Exception e)
            {
                //Handle error if names could not be read
            }
            return tribesmanNames;
        }

        private String RandomName(List<String> tribesmanNames)
        {
            String name = null;

            int iName1 = random.Next(0, tribesmanNames.Count);
            int iName2 = random.Next(0, tribesmanNames.Count);

            String name1 = tribesmanNames.ElementAt<String>(iName1);
            String name2 = tribesmanNames.ElementAt<String>(iName2);
            name2 = name2.ToLower();
            name = name1 + name2;

            return name;
        }

        public List<Assignment> getAssignmentsList()
        {
            List<Assignment> assignments = new List<Assignment>();
            Assignment n = new Assignment();
            n.name = "Nothing";
            Assignment g = new Assignment();
            g.name = "Gather";
            Assignment c = new Assignment();
            c.name = "Craft";
            Assignment b = new Assignment();
            b.name = "Build";
            Assignment f = new Assignment();
        f.name = "Mate";
            Assignment h = new Assignment();
            h.name = "Hunt";
            assignments.Add(n);
            assignments.Add(g);
            assignments.Add(c);
            assignments.Add(b);
            assignments.Add(f);
            assignments.Add(h);
            return assignments;
        }

        public List<Area> getAreaList()
        {
            List<Area> gatherlist = new List<Area>();
            Area claypit = new Area();
            claypit.name = "Claypit";
            claypit.ressourcePrTurn = 5;
            claypit.gatherChance = 80;
            claypit.huntChance = 20;
            claypit.res = findRessource("clay");
            gatherlist.Add(claypit);

            Area field = new Area();
            field.name = "field";
            field.ressourcePrTurn = 7;
            field.gatherChance = 70;
            field.huntChance = 30;
            field.res = findRessource("wheat");
            gatherlist.Add(field);

            Area forrest = new Area();
            forrest.name = "Forrest";
            forrest.ressourcePrTurn = 12;
            forrest.gatherChance = 50;
            forrest.huntChance = 50;
            forrest.res = findRessource("wood");
            gatherlist.Add(forrest);

            Area stonepit = new Area();
            stonepit.name = "Stonepit";
            stonepit.ressourcePrTurn = 5;
            stonepit.gatherChance = 80;
            stonepit.huntChance = 20;
            stonepit.res = findRessource("stone");
            gatherlist.Add(stonepit);

            Area stream = new Area();
            stream.name = "Stream";
            stream.ressourcePrTurn = 5;
            stream.gatherChance = 80;
            stream.huntChance = 20;
            stream.res = findRessource("water");
            gatherlist.Add(stream);

            return gatherlist;
        }

        public List<Building> getBuildingsList()
        {
            List<Building> buildingList = new List<Building>();

            /*Building sh = new Building();
            sh.name = "Storage Hut Expansion";
            sh.size = 1;
            sh.baseGoodPrTurn = -1;
            buildingList.Add(sh);*/

            Building th = new Building();
            th.name = "Tool Hut";
            th.size = 1;
            th.baseGoodPrTurn = 2;
            th.constructionBlocks = 2;
            th.constructionWood = 2;
            th.constructionTools = 1;
            th.construction = 100;
            buildingList.Add(th);

            Building wh = new Building();
            wh.name = "Weapon Hut";
            wh.size = 1;
            wh.baseGoodPrTurn = 2;
            wh.constructionWood = 2;
            wh.constructionBlocks = 2;
            wh.constructionTools = 1;
            wh.construction = 100;
            buildingList.Add(wh);

            Building hb = new Building();
            hb.name = "Hut Builder";
            hb.size = 1;
            hb.baseGoodPrTurn = 2;
            hb.constructionWood = 2;
            hb.constructionBlocks = 2;
            hb.constructionTools = 1;
            hb.construction = 100;
            buildingList.Add(hb);

            return buildingList;
        }

        private void cloneRessource(Ressource res, int amount, StorageHut storageArea)
        {
            for (int i = 0; i < amount; i++)
            {
                Ressource ressource = new Ressource();
                ressource.name = res.name;
                storageArea.addRessource(ressource);
            }
        }

        private Ressource findRessource(String name)
        {
            foreach (Ressource res in ressources)
            {
                if (res.name.ToLower() == name.ToLower())
                    return res;
            }
            return null;

        }

    }
}
